﻿

using HBase.task.Equpt;
using System;
using System.Reflection;
using System.Xml;
namespace HBase {
    public class InitServer {
        public static void start() {
            System.Threading.Tasks.Task.Factory.StartNew(() => {
                init();
            });
        }
        private static void init() {
            String path = "Conf/bean.config";
            XmlDocument doc = new XmlDocument();
            doc.Load(path);
            XmlElement root = doc.DocumentElement;
            XmlNodeList childNodes = root.ChildNodes;
            for(int i= 0;i< childNodes.Count;i++ ) {
                XmlNode node = childNodes[i];
                createNode(node,i);
            }
        }

        static public Type  getType(String value) {

            var assemblyName = value.Substring(0, value.LastIndexOf("."));

            Assembly[] asss = AppDomain.CurrentDomain.GetAssemblies() ;

            foreach (Assembly asst in asss) {
                if (asst.GetName().Name == assemblyName) {

                    Type t = asst.GetType(value);
                    return t;

                }


            }
            String dllName = assemblyName + ".dll";
            String dllPath = AppDomain.CurrentDomain.BaseDirectory + dllName;
            Assembly ass = Assembly.LoadFile(dllPath);//要绝对路径
            Type type = ass.GetType(value);//必须使用名称空间+类名称
            return type;
            
            
        }
        private static void createNode(XmlNode node,int index) {
            if( node.Attributes == null ) {
                return;
            }
            var className = node.Attributes["className"];
            var info = node.Attributes["info"];
            var cpu = node.Attributes["cpu"];
            XmlAttribute p = node.Attributes["p"];
            String value = className.Value;
            try {

                Type type = getType(value);
                if ( node.Name == "task" ) {

                    var t = Activator.CreateInstance(type);
                    ZtTask task = (ZtTask)t;
                    if (cpu != null) {
                        task.cpuId = int.Parse(cpu.Value.ToString());
                    }
             
                    createAndBegin(task ,node,p);
                    if( info != null ) {
                        String v = info.Value;
                        if( !String.IsNullOrEmpty(v) ) {
                            task.stat.className = v;
                        }
                    }
                } else if( node.Name == "factray" ) {
                    ZtFactray obj = ( ZtFactray ) Activator.CreateInstance(type);
               
                    if( p != null ) {
                        obj.setParam(p.Value.ToString());
                    }
                    ZtTask task = obj.create();
                    if( task != null ) {
                        createAndBegin(task ,node,p);
                    }
                }
            } catch( Exception e ) {
                LoggerCommon.startError(InfoCode.start_createTask,value + "创建异常" + e.Message ,e);
            }
        }
        static void createAndBegin(ZtTask task ,XmlNode node, XmlAttribute p ) {
            if (p != null) {
                task.p = p.Value.ToString();
            } 
            TaskPool.add(task);
            if (TaskPool.checkSuperiorIndex(task)) {
                TaskPool.start(task.GetType());
            } else {
                LoggerCommon.fileAll(InfoCode.start_same_superiorIndex, "start_same_superiorIndex" + task.className);
            }
       
        }
    }
}
